from data import PROP_DIC as P1, FLOOR_DIC as P2
from monster import MONSTER_DIC as M
# dic包含地图内所有方格坐标对应的物体属性
# hero_pos为勇士实时坐标


def f(hero_pos, hero_pos_new):
    if hero_pos_new:
        return hero_pos_new
    else:
        return hero_pos


def walk_up(dic, hero_pos, hero, variable_pos, BG, choose):
    try:
        hero_pos_new = meet_event(dic, hero_pos[0], hero_pos[1]-1, hero, variable_pos, BG, choose)
        return f(hero_pos, hero_pos_new)
    except:
        return hero_pos


def walk_down(dic, hero_pos, hero, variable_pos, BG, choose):
    try:
        hero_pos_new = meet_event(dic, hero_pos[0], hero_pos[1]+1, hero, variable_pos, BG, choose)
        return f(hero_pos, hero_pos_new)
    except:
        return hero_pos


def walk_left(dic, hero_pos, hero, variable_pos, BG, choose):
    try:
        hero_pos_new = meet_event(dic, hero_pos[0]-1, hero_pos[1], hero, variable_pos, BG, choose)
        return f(hero_pos, hero_pos_new)
    except:
        return hero_pos


def walk_right(dic, hero_pos, hero, variable_pos, BG, choose):
    try:
        hero_pos_new = meet_event(dic, hero_pos[0]+1, hero_pos[1], hero, variable_pos, BG, choose)
        return f(hero_pos, hero_pos_new)
    except:
        return hero_pos


def simple_attribute_event(p, hero, dic, variable_pos):
    attribute = P1[dic[p]]       # 属性类型
    if 'HP' in attribute:
        hero.HP += P1[dic[p]]['HP']
    if 'attack' in attribute:
        hero.attack += P1[dic[p]]['attack']
    if 'defend' in attribute:
        hero.defend += P1[dic[p]]['defend']
    if 'money' in attribute:
        hero.money += P1[dic[p]]['money']
    if 'key_yellow' in attribute:
        hero.key_yellow += P1[dic[p]]['key_yellow']
    if 'key_blue' in attribute:
        hero.key_blue += P1[dic[p]]['key_blue']
    if 'key_red' in attribute:
        hero.key_red += P1[dic[p]]['key_red']
    if 'level' in attribute:
        hero.level += P1[dic[p]]['level']
    del variable_pos[p]
    return p


def meet_event(dic, x, y, hero, variable_pos, BG, choose):      # 调整判断先后顺序，提升运行效率
    if dic[(x, y)] == '路':      # 触发次数最多，放首位
        return x, y
    elif dic[(x, y)] in M:     # 战斗结算，触发次数也多，放次位
        m_hp, m_attack, m_defend, m_money, m_exp, m_special = M[dic[(x, y)]]
        if hero.attack <= m_defend:
            return None
        elif hero.defend >= m_attack and not m_special:
            if dic[(x, y)] != '血影':
                del variable_pos[(x, y)]
                hero.money += m_money
                hero.exp += m_exp
                return x, y
            else:
                del variable_pos[(x, y)]
                del variable_pos[(x-1, y)]
                del variable_pos[(x+1, y)]
                del variable_pos[(x, y-1)]
                del variable_pos[(x-1, y-1)]
                del variable_pos[(x+1, y-1)]
                del variable_pos[(x, y-2)]
                del variable_pos[(x-1, y-2)]
                del variable_pos[(x+1, y-2)]
                BG.clear = True
                return None
        else:
            HP_ini = hero.HP
            if m_special == '失去25%当前生命':
                hero.HP = int(hero.HP*0.75)
            elif m_special == '固定掉100血':
                if hero.HP < 100:
                    return None
                hero.HP = hero.HP - 100
            elif m_special == '固定掉300血':
                if hero.HP < 300:
                    return None
                hero.HP = hero.HP - 300
            elif m_special == '失去1/3当前生命':
                hero.HP = int(hero.HP * 2 / 3)
            while 1:
                hero.HP -= max(0, (m_attack - hero.defend))
                hero.HP = max(0, hero.HP)
                if not hero.HP:
                    break
                m_hp -= (hero.attack - m_defend)
                m_hp = max(0, m_hp)
                if not m_hp:
                    break
            if hero.HP <= 0:
                hero.HP = HP_ini
                return None
            else:
                if dic[(x, y)] != '血影':
                    del variable_pos[(x, y)]
                    hero.money += m_money
                    hero.exp += m_exp
                    return x, y
                else:
                    del variable_pos[(x, y)]
                    del variable_pos[(x - 1, y)]
                    del variable_pos[(x + 1, y)]
                    del variable_pos[(x, y - 1)]
                    del variable_pos[(x - 1, y - 1)]
                    del variable_pos[(x + 1, y - 1)]
                    del variable_pos[(x, y - 2)]
                    del variable_pos[(x - 1, y - 2)]
                    del variable_pos[(x + 1, y - 2)]
                    BG.clear = True
                    return None
    elif dic[(x, y)] == '黄门':
        if hero.key_yellow > 0:
            return simple_attribute_event((x, y), hero, dic, variable_pos)
        else:
            return None
    elif dic[(x, y)] == '蓝门':
        if hero.key_blue > 0:
            return simple_attribute_event((x, y), hero, dic, variable_pos)
        elif BG.key_shop_5 and hero.money >= 50:
            hero.money -= 50
            del variable_pos[(x, y)]
            return x, y
        else:
            return None
    elif dic[(x, y)] == '红门':
        if hero.key_red > 0:
            return simple_attribute_event((x, y), hero, dic, variable_pos)
        elif BG.key_shop_5 and hero.money >= 100:
            hero.money -= 100
            del variable_pos[(x, y)]
            return x, y
        else:
            return None
    elif dic[(x, y)] in P1:     # 捡东西触发次数也多，但不能放门前面判断，否则覆盖门分支
        return simple_attribute_event((x, y), hero, dic, variable_pos)
    elif dic[(x, y)] in P2:         # 楼层变化判定
        if dic[(x, y)] != '下22楼':
            BG.floor += P2[dic[(x, y)]]
        else:
            BG.floor = 22
        BG.base = choose(BG.floor)
        return BG.base.hero_pos
    elif dic[(x, y)] in {'墙', '绿门'}:    # 撞墙次数可能挺多的，放中间吧
        return None
    elif dic[(x, y)] in {'十字架', '榔头', '冰之灵杖', '炎之灵杖', '心之灵杖'}:
        BG.prop.append(dic[(x, y)])
        print('获得', dic[(x, y)])
        del variable_pos[(x, y)]
        return None
    elif dic[(x, y)] == '仙子':
        if (x, y) == (5, 8):
            variable_pos[(4, 8)] = variable_pos[(5, 8)]
            del variable_pos[(x, y)]
        elif (x, y) == (4, 8):
            if '十字架' in BG.prop:
                hero.level += 3
                hero.HP = int(hero.HP*4/3)
                hero.attack = int(hero.attack*4/3)
                hero.defend = int(hero.defend*4/3)
                choose(20).open_up = True
                print('20楼上楼已显示')
                BG.prop.remove('十字架')
                del variable_pos[(x, y)]    # 删除机制，所以要开隐藏楼在到16楼前不能捡十字架
            if '冰之灵杖' in BG.prop:
                print('已开启22层')
        else:
            print(BG.prop)
            if '炎之灵杖' in BG.prop and '心之灵杖' in BG.prop:
                floor23 = choose(23)
                del floor23.variable_pos[(5, 6)]
                print('23楼下绿门已开')
                BG.prop.remove('冰之灵杖')
                BG.prop.remove('炎之灵杖')
                BG.prop.remove('心之灵杖')
                print('已开启24层')
        return None
    elif dic[(x, y)] == '小偷':
        floor2 = choose(2)
        floor18 = choose(18)
        if '绿门' in floor2.dic.values():
            del floor2.variable_pos[(1, 6)]
            print('2楼绿门已开')
        elif '榔头' in BG.prop:
            del floor18.variable_pos[(5, 8)]
            del floor18.variable_pos[(5, 9)]
            BG.prop.remove('榔头')
            print('18楼公主房间道路已通')
        return None
    elif dic[(x, y)] == '公主':
        if not '上楼' in BG.base.dic.values():
            BG.base.open_up = True
            print('18楼上楼已显示')
        return None
    # 以下均为只触发1次的分支，都放后面，若商店类补上手动购买逻辑，则分支可提前至开门以上
    elif dic[(x, y)] == '3楼商店':     # 商店整合判定失败，一个个判定吧
        if not BG.money_shop_3:
            BG.money_shop_3 = True
            print('3楼商店已激活')
        return None
    elif dic[(x, y)] == '5楼老头':
        if not BG.exp_shop_5:
            BG.exp_shop_5 = True
            print('5楼老头已激活')
        return None
    elif dic[(x, y)] == '5楼老太':
        if not BG.key_shop_5:
            BG.key_shop_5 = True
            print('5楼老太已激活')
        return None
    elif dic[(x, y)] == '11楼商店':
        if not BG.money_shop_11:
            BG.money_shop_11 = True
            BG.money_shop_3 = False
            print('11楼商店已激活')
        return None
    elif dic[(x, y)] == '13楼老头':
        if not BG.exp_shop_13:
            BG.exp_shop_13 = True
            print('13楼老头已激活')
        return None
    elif dic[(x, y)] == '15楼老头':
        if hero.exp >= 500:
            hero.attack += 120
            hero.exp -= 500
            del variable_pos[(x, y)]
        return None
    elif dic[(x, y)] == '15楼老太':
        if hero.money >= 500:
            hero.defend += 120
            hero.money -= 500
            del variable_pos[(x, y)]
        return None
    elif dic[(x, y)] == '圣水':
        hero.HP *= 2
        del variable_pos[(x, y)]
        return x, y
    else:   # 暂时没想到还漏了什么，当路处理吧
        return x, y
